


import { _AsyncData } from 'nuxt3/dist/app/composables/asyncData'
// import baseUrl from './baseUrl'
// import { ElMessage } from 'element-plus'

let baseUrl = 'http://saas-official-api.zbase.cn'
// 指定后端返回的基本数据类型
export interface ResponseConfig {
    code: number,
    status: number,
    data: any,
    msg: string
}
export interface ValueConfig {
    value: any,
 
}
 
const fetch = (url: string, options?: any): Promise<any>  => {
    const reqUrl = baseUrl + url
    return new Promise((resolve, reject) => {
        useFetch(reqUrl, { ...options }).then(({ data, error }: _AsyncData) => {
            if (error.value) {
                reject(error.value)
                return
            }
            const value = data.value
            if (!value) {
                // 这里处理错误回调
                // reject(value)
                // $router.replace('/reject/' + value.status)
            }else if(value.code == 102){
                // ElMessage({
                //     message: value.msg,
                //     type: 'error',
                // })
            } else {
                resolve(ref(value))
            }
        }).catch((err: any) => {
            reject(err)
        })
    })
}
 
const http = new class Http {

    request({
        url,
       ...options
    }:{url:string;[propsName:string]:any}){
      return new Promise(async(resolve,reject)=> {
        try {
            const res = await fetch(url, options);
            return resolve(res.value) 
        } catch (error) {
            reject(error)
        }
      }
      )
    }
 
    get(url: string, params?: any): Promise<any> {
        return fetch(url, { method: 'get', params })
    }
 
    post(url: string, params?: any): Promise<any>  {
        return fetch(url, { method: 'post', params })
    }
 
    put(url: string, body?: any): Promise<any>  {
        return fetch(url, { method: 'put', body })
    }
 
    delete(url: string, body?: any): Promise<any>  {
        return fetch(url, { method: 'delete', body })
    }
}

export {http}